import sys
sys.path.append('../kernel')
from model import Model
class InfoModel(Model):
    def __init__(self):
        self.table='cg_info'
        #链接数据库
        super().__init__()
    #添加采购信息
    def add_info(self,save_data):
        title=save_data['title']
        t_id=save_data['t_id']
        url=save_data['url']
        creation_time=save_data['creation_time']
        c_id=save_data['c_id']
        # 执行查询
        sql = "INSERT INTO " + self.table + " SET title=%s,t_id=%s,url=%s,creation_time=%s,c_id=%s"
        self.cursor.execute(sql,[title,t_id,url,creation_time,c_id])
        self.conn.commit()  # 提交
    def exists(self,url):
        sql = "SELECT id FROM " + self.table + " WHERE url=%s"
        self.cursor.execute(sql,[url])
        data = self.cursor.fetchone()
        return data

    def get_title(self,id):
        sql = "SELECT title FROM " + self.table + " WHERE id>%s LIMIT 1000"
        self.cursor.execute(sql,[id])
        data = self.cursor.fetchall()
        return data

    def __del__(self):
        #计数数据库链接
        self.cursor.close()
        self.conn.close()